<template>
    <el-dialog
            :title="!dataForm.costId ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                <el-form-item label="任务单编号" prop="taskNumber">
                        <el-input v-model="dataForm.taskNumber" placeholder="任务单编号"></el-input>
                </el-form-item>
                <el-form-item label="运费金额" prop="freightAmount">
                        <el-input v-model="dataForm.freightAmount" placeholder="运费金额"></el-input>
                </el-form-item>
                <el-form-item label="附加费金额" prop="additionalFeeAmount">
                        <el-input v-model="dataForm.additionalFeeAmount" placeholder="附加费金额"></el-input>
                </el-form-item>
                <el-form-item label="过路费金额" prop="tollAmount">
                        <el-input v-model="dataForm.tollAmount" placeholder="过路费金额"></el-input>
                </el-form-item>
                <el-form-item label="总费用金额" prop="totalCostAmount">
                        <el-input v-model="dataForm.totalCostAmount" placeholder="总费用金额"></el-input>
                </el-form-item>
                <el-form-item label="费用状态" prop="costStatus">
                        <el-input v-model="dataForm.costStatus" placeholder="费用状态"></el-input>
                </el-form-item>
                 <el-form-item label="费用状态" prop="costStatus">
                    <el-select v-model="dataForm.costStatus" placeholder="请选择费用状态">
                        <el-option label="未支付" value="未支付"></el-option>
                        <el-option label="已支付" value="已支付"></el-option>
                    </el-select>
                </el-form-item>  
                <el-form-item label="支付时间" prop="paymentTime">
                        <el-date-picker
                                v-model="dataForm.paymentTime"
                                type="datetime"
                                placeholder="支付时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            costId: null,
            taskNumber: null,
            freightAmount: null,
            additionalFeeAmount: null,
            tollAmount: null,
            totalCostAmount: null,
            costStatus: null,
            paymentTime: null,
            createdAt: null,
            updatedAt: null
          },
          dataRule: {
            costId: [
                        { required: true, message: '费用ID（主键）不能为空', trigger: 'blur' }
            ],
            taskNumber: [
                        { required: true, message: '任务单编号不能为空', trigger: 'blur' }
            ],
            freightAmount: [
                        { required: true, message: '运费金额不能为空', trigger: 'blur' }
            ],
            additionalFeeAmount: [
                        { required: true, message: '附加费金额不能为空', trigger: 'blur' }
            ],
            tollAmount: [
                        { required: true, message: '过路费金额不能为空', trigger: 'blur' }
            ],
            totalCostAmount: [
                        { required: true, message: '总费用金额不能为空', trigger: 'blur' }
            ],
            costStatus: [
                        { required: true, message: '费用状态不能为空', trigger: 'blur' }
            ],
            paymentTime: [
                        { required: true, message: '支付时间不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.costId = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.costId = id || 0
            if (this.dataForm.costId) {
              this.$http({
                url: this.$http.adornUrl(`/app/transportation_costs/info/${this.dataForm.costId}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.apptransportationcosts
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/transportation_costs/${!this.dataForm.costId ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>